#include "Intent.h"

CIntent::CIntent()
{
}

CIntent::~CIntent()
{
}

int CIntent::Release()
{
	lock();
	m_nRef--;
	if (m_nRef == 0) {
		unlock();
		delete this;

		return 0;
	}
	else {
		unlock();
	}

	return m_nRef;
}

int CIntent::encoding(char * pszBuffer, const int nBufferSize)
{
	int nRet = 0;
	int pos = 0;

	nRet = encodingString(pszBuffer, nBufferSize, pos, m_strAction);
	if (nRet < 0) {
		return nRet;
	}

	nRet = encodingString2IntMap(pszBuffer, nBufferSize, pos, m_mapInt);
	if (nRet < 0) {
		return nRet;
	}

	nRet = encodingString2FloatMap(pszBuffer, nBufferSize, pos, m_mapFloat);
	if (nRet < 0) {
		return nRet;
	}

	nRet = encodingString2DoubleMap(pszBuffer, nBufferSize, pos, m_mapDouble);
	if (nRet < 0) {
		return nRet;
	}

	nRet = encodingString2BooleanMap(pszBuffer, nBufferSize, pos, m_mapBoolean);
	if (nRet < 0) {
		return nRet;
	}

	nRet = encodingString2StringMap(pszBuffer, nBufferSize, pos, m_mapString);
	if (nRet < 0) {
		return nRet;
	}

	nRet = encodingString2ObjectMap(pszBuffer, nBufferSize, pos, m_mapObject);
	if (nRet < 0) {
		return nRet;
	}

	return pos;
}

int CIntent::decoding(const char * pBuffer, const int nBufferSize)
{
	int pos = 0;
	int nRet = 0;

	nRet = decodingString(pBuffer, nBufferSize, pos, m_strAction);
	if (nRet < 0) {
		return nRet;
	}

	nRet = decodingString2IntMap(pBuffer, nBufferSize, pos, m_mapInt);
	if (nRet < 0) {
		return nRet;
	}

	nRet = decodingString2FloatMap(pBuffer, nBufferSize, pos, m_mapFloat);
	if (nRet < 0) {
		return nRet;
	}

	nRet = decodingString2DoubleMap(pBuffer, nBufferSize, pos, m_mapDouble);
	if (nRet < 0) {
		return nRet;
	}

	nRet = decodingString2BooleanMap(pBuffer, nBufferSize, pos, m_mapBoolean);
	if (nRet < 0) {
		return nRet;
	}

	nRet = decodingString2StringMap(pBuffer, nBufferSize, pos, m_mapString);
	if (nRet < 0) {
		return nRet;
	}

	nRet = decodingString2ObjectMap(pBuffer, nBufferSize, pos, m_mapObject);
	if (nRet < 0) {
		return nRet;
	}

	return pos;
}

void CIntent::setAction(const char *pszAction)
{
	m_strAction = pszAction;
}

std::string & CIntent::getAction()
{
	return m_strAction;
}

void CIntent::putInt(const char *pszName, int value)
{
	m_mapInt[pszName] = value;
}

void CIntent::putFloat(const char *pszName, float value)
{
	m_mapFloat[pszName] = value;
}

void CIntent::putDouble(const char *pszName, double value)
{
	m_mapDouble[pszName] = value;
}

void CIntent::putBoolean(const char *pszName, bool value)
{
	m_mapBoolean[pszName] = value;
}

void CIntent::putString(const char *pszName, const char * value)
{
	m_mapString[pszName] = value;
}

void CIntent::putObject(const char *pszName, CBaseObject *value)
{
	m_mapObject[pszName] = value;
}

bool CIntent::getInt(const char *pszName, int &value)
{
	auto itor = m_mapInt.find(pszName);
	if (itor == m_mapInt.end()) {
		return false;
	}

	value = itor->second;
	return true;
}

bool CIntent::getFloat(const char *pszName, float &value)
{
	auto itor = m_mapFloat.find(pszName);
	if (itor == m_mapFloat.end()) {
		return false;
	}

	value = itor->second;
	return true;
}

bool CIntent::getDouble(const char *pszName, double &value)
{
	auto itor = m_mapDouble.find(pszName);
	if (itor == m_mapDouble.end()) {
		return false;
	}

	value = itor->second;
	return true;
}

bool CIntent::getBoolean(const char *pszName, bool &value)
{
	auto itor = m_mapBoolean.find(pszName);
	if (itor == m_mapBoolean.end()) {
		return false;
	}

	value = itor->second;
	return true;
}

bool CIntent::getString(const char *pszName, std::string &value)
{
	auto itor = m_mapString.find(pszName);
	if (itor == m_mapString.end()) {
		return false;
	}

	value = itor->second;
	return true;
}

bool CIntent::getObject(const char *pszName, CBaseObject *& value)
{
	auto itor = m_mapObject.find(pszName);
	if (itor == m_mapObject.end()) {
		return false;
	}

	value = itor->second;
	return true;
}

void CIntent::ObjectsAddRef()
{
	for (auto item : m_mapObject) {
		CBaseObject *pObject = static_cast<CBaseObject*>(item.second);
		pObject->AddRef();
	}
}

void CIntent::ObjectsRelease()
{
	for (auto item : m_mapObject) {
		((CBaseObject*)item.second)->Release();
	}
}
